//店面管理-店面里的柜子的格子分布
App.util.ns('Admin.Storefront.Schedule');
Admin.Storefront.Schedule.Schedulegoods = function(dataTable){
    var win = new App.Win({
        name: 'schedulegoods',
        title: '格子分布',
        width: '720px'
    });
    //列表
    win.dataTable = new App.DataTable({
        baseUrl: '/admin/cabinet/cell',
        dataUrl: 'lists',
        table: {
            id: 'id',
            main: 1,
            showId: false,
            withSelect: false,
            columns: [
                {name: '位置', width: '90px', value: function(item){
                    return item.row + ' 行，' + item.col + ' 列';
                }},
                {name: '原商品', width: '90px', value: function(item){
                    var name = item.goods ? item.goods.name : '未分配';
                    return $('<span style="margin: 0 6px">'+name+'</span>');
                }},
                {name: '调换商品', width: '350px', value: function(item){
                    var selector = new App.Form.SelectLists({
                        type: 'selectLists',
                        name: 'id',
                        title: '期望商品',
                        data_id: 'goods_id',
                        width: '600px',
                        data_name: function(item){
                            return item.id+': '+item.name;
                        },
                        dataTable: {
                            baseUrl: '/admin/goods/goods',
                            dataUrl: 'lists?status=1',
                            data_id: 'id',
                            data_name: 'name',
                            table: {
                                id: 'id',
                                main: 0,
                                columns: [
                                    {name: '商品名', value: 'name'}
                                ]
                            },
                            filter: [
                                {type: 'keywords'}
                            ]
                        },
                        detail: {
                            form: {
                                items: App.util.copy(Admin.Goods.Goods.Conf.form)
                            }
                        }
                    });
                    selector.dataTable.win.fix(win.frame);
                    if(selector.detail){
                        selector.detail.win.fix(win.frame);
                    }

                    var goodsjson =win.schedule.goods_json;
                    if(goodsjson){
                        var goodsArr = JSON.parse(goodsjson);
                        for(var i=0;i<goodsArr.length;i++){
                            if(goodsArr[i].id == item.id){
                                selector.change(goodsArr[i].target.id, goodsArr[i].target.name, goodsArr[i].target);
                                break;
                            }
                        }
                    }
                    return selector.frame;
                }},
            ]
        },
        afterLoadData: function(self){
            self.frame.find('tbody td').css({padding: '0'});
        },
        after: function(self){
            self.frame.find('> .header').remove();
            self.frame.find('.data-table').addClass('table-bordered').removeClass('table-hover');
            self.fix(win.contentContainer);
        }
    });
    win.changeSchedule = function(schedule){
        win.schedule = schedule;
        win.setTitle('格子分布 - '+schedule.name);
        win.dataTable.param.set('cabinet_id', schedule.cabinet_id);
        win.dataTable.loadData();
        return win;
    };

    //设置按钮
    win.setBtns([(function(){
        var cancelBtn = $('<a class="btn btn-danger btn-xs m-right-sm">取消</a>');
        cancelBtn.click(function(){
            win.hide();
        });
        return cancelBtn;
    })(), (function(){
        var saveBtn = $('<a class="btn btn-success btn-xs">提交</a>');
        saveBtn.click(function(){
            var goodsJson = [];
            win.dataTable.frame.find('tbody tr').each(function(){
                var target = $(this).find('input.select-lists-data').val();
                var item = JSON.parse($(this).attr('data-data'));
                item.target = target ? JSON.parse(target) : '';
                goodsJson.push(item);
            });
            App.util.request.postData({
                url: '/admin/storefront/schedule/updateGoodJsonFeild',
                data: {
                    id: win.schedule.id,
                    goods_json: JSON.stringify(goodsJson)
                },
                loading: new App.Loading(win.contentContainer),
                success: function(){
                    dataTable.loadData(true);
                    App.util.hint.toast({
                        type: 'ok',
                        text: '提交成功'
                    }).delay(500, function(){
                        win.hide();
                    });
                }
            });
        });
        return saveBtn;
    })()]);
    win.hide();
    return win;
};